package com.example.yuejian21.nongfusanquan.yuejian21.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yuejian21.nongfusanquan.yuejian21.R;
import com.example.yuejian21.nongfusanquan.yuejian21.adapter.CommentAdapter;
import com.example.yuejian21.nongfusanquan.yuejian21.javabean.Commentbean;
import com.example.yuejian21.nongfusanquan.yuejian21.model.Commentmodel;
import com.squareup.picasso.Picasso;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Nongfusanquan on 2017-10-20.
 */

public class PostdetailActivity extends AppCompatActivity {

    private ImageView usericon;
    private ImageView pphoto;
    private TextView title1;
    private TextView name1;
    private TextView creattime;
    private Button back_postdetial,submit_comment;
    private RecyclerView recyclerView;
    String p_id,uid,comment;
    private EditText comment_ed;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_postdetail);
        initView();
        initEvent();
        getData();
        getCommentList();


    }


    private void initView() {
        usericon = (ImageView) findViewById(R.id.dynamicdet_icon);
        pphoto = (ImageView) findViewById(R.id.dynamicdet_pic);
        title1 = (TextView) findViewById(R.id.dynamicdet_info);
        name1 = (TextView) findViewById(R.id.dynamicdet_uname);
        creattime = (TextView) findViewById(R.id.dynamicdet_creattime);
        back_postdetial = (Button)findViewById(R.id.back_postdetail);
        submit_comment =(Button)findViewById(R.id.submit_comment);
        comment_ed = (EditText)findViewById(R.id.addcomment_et);
        recyclerView=(RecyclerView)findViewById(R.id.rv);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));//设置布局

    }

    private void initEvent(){
        back_postdetial.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        submit_comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doComment();
            }
        });
    }
    private void getData() {
        Bundle bundle = this.getIntent().getExtras();
        String username = bundle.getString("u_name");
        String pcontent = bundle.getString("p_info");
        p_id = bundle.getString("p_id");
        String ctime = bundle.getString("p_createtime");
        String uicon = "http://10.0.2.2/yuejian14/Public/Admin/postphoto/"+bundle.getString("u_pic");
        String ppic = "http://10.0.2.2/yuejian14/Public/Admin/postphoto/"+bundle.getString("p_pic");

        name1.setText(username);
        title1.setText(pcontent);
        creattime.setText(ctime);
        Picasso.with(this).load(uicon).into(usericon);
        Picasso.with(this).load(ppic).into(pphoto);

    }
    private void getCommentList() {
        Commentmodel activitymodel=new Commentmodel();
        Call<List<Commentbean>> call=activitymodel.getCommentlist(p_id);
        call.enqueue(new Callback<List<Commentbean>>() {
            @Override
            public void onResponse(Call<List<Commentbean>> call, Response<List<Commentbean>> response) {
                List<Commentbean> data = response.body();  //获取数据
                recyclerView.setAdapter(new CommentAdapter(PostdetailActivity.this, data));


            }

            @Override
            public void onFailure(Call<List<Commentbean>> call, Throwable t) {

            }
        });

    }
    private void doComment() {
        SharedPreferences sp = this.getSharedPreferences("cache", Context.MODE_APPEND);
        uid = sp.getString("uid","0");
        comment=comment_ed.getText().toString();
        Commentmodel commentModel = new Commentmodel();
        Call<Commentbean> call = commentModel.addComment(p_id,uid,comment);
        call.enqueue(new Callback<Commentbean>() {
            @Override
            public void onResponse(Call<Commentbean> call, Response<Commentbean> response) {
                if ("1".equals(String.valueOf(response.body().getRes()))){
                    comment_ed.setText(null);
                    Toast.makeText(PostdetailActivity.this,"评论成功",Toast.LENGTH_SHORT).show();

                    getCommentList();
                }
                else {
                    Toast.makeText(PostdetailActivity.this,"评论失败",Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<Commentbean> call, Throwable t) {

            }
        });

    }

}


